package de.tabuma.pm.design;

import de.tabuma.lib.*;
import de.tabuma.lib.contentmanagement.entity.*;
import de.tabuma.pm.security.controller.*;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.users.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.6.1 $
 * @since $Date: 2010/01/13 10:19:20 $
 */
public class DesignController extends tabumacontroller {

    private static Logger log = Logger.getLogger(DesignController.class.getName());
    public DesignController(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }
    
    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        try {
            out.println("<div class=\"contentContent\">If no design is set, there is a standard design.</div>");

            String entityName = "Design";
            String cssUrl = "CSS Url";
            String designTyp = "Design Typ";
            String name = "Design Name";

            Content content = new Content();

            Vector<Field> editableFields = new Vector<Field>();
            editableFields.add(Design.class.getDeclaredField("name"));
            editableFields.add(Design.class.getDeclaredField("cssUrl"));
            editableFields.add(Design.class.getDeclaredField("designType"));
            content.FormContent.setEditableFields(editableFields);

            Vector<Integer> editableTypes = new Vector<Integer>();
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_LIST));
            content.FormContent.setEditableFieldTypes(editableTypes);
            content.FormContent.setEntityClass(Design.class);
            content.FormContent.setEntityName(entityName);
            content.FormContent.setFacadeObject(new DesignFacade());

            Vector<Field> mandatoryField = new Vector<Field>();
            mandatoryField.add(Design.class.getDeclaredField("name"));
            mandatoryField.add(Design.class.getDeclaredField("designType"));
            content.FormContent.setMandatoryField(mandatoryField);

            UserService userService = UserServiceFactory.getUserService();
            com.google.appengine.api.users.User user = userService.getCurrentUser();
            String projectId = request.getParameter(de.tabuma.pm.constants.projectId);
            content.ListContent.setListParameterValue(""+projectId);

            Vector<Field> staticFields = new Vector<Field>();
            staticFields.add(Design.class.getDeclaredField("projectId"));
            content.FormContent.StaticContent.setStaticFields(staticFields);

            Vector<Integer> staticFieldsType = new Vector<Integer>();
            staticFieldsType.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_INTEGER));
            content.FormContent.StaticContent.setStaticFieldsTyp(staticFieldsType);

            Vector<String> staticFieldsValue = new Vector<String>();
            staticFieldsValue.add(projectId);
            content.FormContent.StaticContent.setStaticFieldsValue(staticFieldsValue);
            
            content.GeneralContent.setParamName(de.tabuma.pm.constants.DESIGN_PARAM);
            
            Vector<Field> listableFields = new Vector<Field>();
            listableFields.add(Design.class.getDeclaredField("name"));
            content.ListContent.setListableFields(listableFields);
            
            content.GeneralContent.setServlet(this.getServlet());
            content.GeneralContent.setParameter(this.getParameter());
            
            Hashtable<Field, String> displayFields = new Hashtable<Field, String>();
            displayFields.put(Design.class.getDeclaredField("name"), name);
            displayFields.put(Design.class.getDeclaredField("cssUrl"), cssUrl);
            displayFields.put(Design.class.getDeclaredField("designType"), designTyp);
            content.FormContent.setDisplayedName(displayFields);
            
            Hashtable<Field, List> listParameterContent = new Hashtable<Field, List>();
            List myList = new ArrayList();
            myList.add(new DesignType(DesignType.TYPE_RIBBON_MENU,"Ribbon Design"));
            myList.add(new DesignType(DesignType.TYPE_CSS_DROP_DOWN_MENU,"CSS Drop Down Menu Design"));
            myList.add(new DesignType(DesignType.TYPE_ACCORDING_MENU,"According Menu Design"));
            myList.add(new DesignType(DesignType.TYPE_TAB_MENU,"Tab Menu Design"));
            myList.add(new DesignType(DesignType.TYPE_NAVBAR,"Navigation Bar Menu Design"));
            listParameterContent.put(Design.class.getDeclaredField("designType"), myList);
            content.ListContent.setListParameterContent(listParameterContent);

            Hashtable<Field,List<Field>> listParameterContentDisplayedFields = new Hashtable<Field, List<Field>>();
            List<Field> displayFieldList = new ArrayList<Field>();
            displayFieldList.add(DesignType.class.getDeclaredField("name"));
            listParameterContentDisplayedFields.put(Design.class.getDeclaredField("designType"), displayFieldList);
            content.ListContent.setListParameterDisplayedField(listParameterContentDisplayedFields);

            content.GeneralContent.setUser(user.getEmail());

            out.println(content.navigate(request));

        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }
}